package org.dromara.xhlj.crud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dromara.common.satoken.utils.LoginHelper;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xhlj.domain.vo.XhljActivityTaskVo;
import org.dromara.xhlj.domain.query.XhljActivityTaskQuery;
import org.dromara.xhlj.domain.bo.XhljActivityTaskBo;
import org.dromara.xhlj.crud.service.IXhljActivityTaskCrudService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * xhlj-crud-活动任务
 *
 * @author yf
 * @date 2024-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xhlj/activityTask")
public class XhljActivityTaskCrudController extends BaseController {

    private final IXhljActivityTaskCrudService xhljActivityTaskService;

    /**
     * 分页查询活动任务列表
     */
    @SaCheckPermission("xhlj:activityTask:list")
    @GetMapping("/list")
    public TableDataInfo<XhljActivityTaskVo> list(XhljActivityTaskQuery query, PageQuery pageQuery) {
         return xhljActivityTaskService.queryPageList(query, pageQuery);
    }

    /**
     * 查询活动任务列表
     */
    @SaCheckPermission("xhlj:activityTask:list")
    @GetMapping("/allList")
    public TableDataInfo<XhljActivityTaskVo> allList(XhljActivityTaskQuery query) {
        List<XhljActivityTaskVo> list =  xhljActivityTaskService.queryList(query);
        return TableDataInfo.build(list);
    }


    /**
     * 导出活动任务列表
     */
    @SaCheckPermission("xhlj:activityTask:export")
    @Log(title = "活动任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XhljActivityTaskQuery query, HttpServletResponse response) {
        List<XhljActivityTaskVo> list = xhljActivityTaskService.queryList(query);
        ExcelUtil.exportExcel(list, "活动任务", XhljActivityTaskVo.class, response);
    }

    /**
     * 获取活动任务详细信息
     *
     * @param taskId 主键
     */
    @SaCheckPermission("xhlj:activityTask:query")
    @GetMapping("/{taskId}")
    public R<XhljActivityTaskVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer taskId) {
        return R.ok(xhljActivityTaskService.queryById(taskId));
    }

    /**
     * 新增活动任务
     */
    @SaCheckPermission("xhlj:activityTask:add")
    @Log(title = "活动任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XhljActivityTaskBo bo) {
        bo.setCreateBy(Math.toIntExact(LoginHelper.getUserId()));
        return toAjax(xhljActivityTaskService.insertByBo(bo));
    }

    /**
     * 修改活动任务
     */
    @SaCheckPermission("xhlj:activityTask:edit")
    @Log(title = "活动任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XhljActivityTaskBo bo) {
        return toAjax(xhljActivityTaskService.updateByBo(bo));
    }

    /**
     * 删除活动任务
     *
     * @param taskIds 主键串
     */
    @SaCheckPermission("xhlj:activityTask:remove")
    @Log(title = "活动任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] taskIds) {
        return toAjax(xhljActivityTaskService.deleteWithValidByIds(List.of(taskIds), true));
    }
}
